package com.lzb.watermarkcamera.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.watermarkcamera.BaseAc;
import com.lzb.watermarkcamera.R;
import com.lzb.watermarkcamera.adapter.ResolutionAdapter;
import com.lzb.watermarkcamera.bean.ResolutionBean;
import com.lzb.watermarkcamera.databinding.ActivityPictureResolutionBinding;
import com.lzb.watermarkcamera.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class PictureResolutionActivity extends BaseAc<ActivityPictureResolutionBinding> implements OnItemClickListener {

    private ResolutionAdapter resolutionAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.activity_picture_resolution;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPictureResolutionBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.rvResolutionList.setLayoutManager(new LinearLayoutManager(mContext));
        resolutionAdapter = new ResolutionAdapter();
        mDataBinding.rvResolutionList.setAdapter(resolutionAdapter);
        resolutionAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        resolutionAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        resolutionAdapter.getItem(position).setSelected(true);
        resolutionAdapter.notifyDataSetChanged();
        AppSpUtils.saveResolution(resolutionAdapter.getItem(position));
    }

    @Override
    protected void initData() {
        List<ResolutionBean> list = new ArrayList<>();
        list.add(new ResolutionBean("超清", 4096, 3072, "4096x3072，约12.00MB"));
        list.add(new ResolutionBean("普通", 3264, 1836, "3264x1836，约5.72MB"));
        list.add(new ResolutionBean("低清", 1920, 1080, "1920x1080，约1.98MB"));
        ResolutionBean resolutionBean = AppSpUtils.getResolution();
        for (int i = 0; i < list.size(); i++) {
            ResolutionBean bean = list.get(i);
            if (resolutionBean.getTitle().equals(bean.getTitle())) {
                oldPosition = i;
                bean.setSelected(true);
                break;
            }
        }
        resolutionAdapter.setList(list);
    }
}